package com.fysports.web.model.app;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.fysports.web.model.BaseModel;


/**
 * 系统升级
 * Created by konbluesky
 * Date : 15/6/18 下午10:53
 * Project :jdmdata_server
 */
public class AppUpdate extends BaseModel<AppUpdate> {
    private static Log log = Log.getLog(AppUpdate.class);
    public final static AppUpdate dao = new AppUpdate();
    public final static String COL_VERSION = "version";


    public AppUpdate() {
        if (this.getStr(COL_UUID) == null || this.getStr(COL_UUID).length() == 0) {
            this.set(COL_UUID, generateUUID());
        }
    }


    /**
     * 获取最新版本的记录
     *
     * @return
     */
    public AppUpdate getNewUpdate() {
        String sql = "select * from " + getTable().getName() + " where version=(select max(version) from "
                + getTable().getName() + ")";
        return findFirst(sql);
    }


    /**
     * 设置更新生效记录（同一时刻只有一条记录可用）
     *
     * @param id
     */
    public int setEffect(String id) {
        setAllflag(false);
        String sql = "UPDATE " + getTable().getName() + " SET liveflag = 1 where uuid=?";
        return Db.update(sql, id);
    }


    /**
     * 设置全部记录有效或无效
     *
     * @param flag
     * @return
     */
    public int setAllflag(boolean flag) {
        int i = flag ? 1 : 0;
        String sql = "UPDATE " + getTable().getName() + " SET liveflag = ?";
        return Db.update(sql, i);
    }
}